home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / hs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-30  |  18.4 KB  |  755 lines

  1. /* Interface driver for the DRSI PCPA or the Eagle 8530 boards for the IBM PC
  2.  * connected to a WA4DSY 56kbps modem. Uses polling-loop transfers with
  3.  * interrupts disabled for maximum speed.
  4.  *
  5.  * This driver is a bit of a kludge. A DMA-driven card and driver (e.g.,
  6.  * the PI) is much better, but this is better than nothing if all you have
  7.  * is a "dumb" 8530 card.
  8.  *
  9.  * Copyright 1991 Phil Karn, KA9Q
  10.  */
  11. #include <stdio.h>
  12. #include <dos.h>
  13. #include "global.h"
  14. #include "mbuf.h"
  15. #include "iface.h"
  16. #include "pktdrvr.h"
  17. #include "netuser.h"
  18. #include "hs.h"
  19. #include "z8530.h"
  20. #include "ax25.h"
  21. #include "trace.h"
  22. #include "pc.h"
  23. #include "proc.h"
  24. #include "devparam.h"
  25.  
  26. static void flushrx __ARGS((int16 data));
  27. static void hdlcparam __ARGS((struct hdlc *hp));
  28. static void hexint __ARGS((struct hdlc *hp));
  29. static void hrxint __ARGS((struct hdlc *hp));
  30. static int hs_stop __ARGS((struct iface *iface));
  31. static int hs_raw __ARGS((struct iface *iface,struct mbuf *bp));
  32. static void hs_tx __ARGS((int unused,void *hp1,void *a));
  33. static int32 hs_ctl __ARGS((struct iface *,int cmd,int set,int32 val));
  34. static void hstxoff __ARGS((struct hdlc *hp));
  35. static void hstxon __ARGS((struct hdlc *hp));
  36. static void htxint __ARGS((struct hdlc *hp));
  37. static void init_delay __ARGS((void));
  38. static void msdelay __ARGS((void));
  39.  
  40. static struct hs Hs[NHS];
  41. static INTERRUPT (*Hshandle[])() = { hs0vec };
  42. static struct hdlc Hdlc[2*NHS];
  43. static int16 Nhs;
  44.  
  45. /* Master interrupt handler for the PC-100 card. All interrupts come
  46.  * here first, then are switched out to the appropriate routine.
  47.  */
  48. void
  49. hsint(dev)
  50. int dev;
  51. {
  52.     register char iv;
  53.     int16 hsbase;
  54.     register struct hdlc *hp;
  55.     
  56.     Hs[dev].ints++;
  57.     hsbase = Hs[dev].addr;
  58.  
  59. #ifdef    foo
  60.     outportb(hsbase+4,0x8+0x10);    /* HIT EAGLE INTACK */
  61.     (void)inportb(hsbase+CHANA+CTL,R0);
  62.     outportb(hsbase+4,0x8);        /***/
  63. #endif
  64.  
  65.     /* Read interrupt status from channel A */
  66.     while((iv = read_scc(hsbase+CHANA+CTL,R3)) != 0){
  67.         if(iv & CHARxIP){
  68.             /* Channel A Rcv Interrupt Pending */
  69.             hp = &Hdlc[2*dev];
  70.             hrxint(hp);
  71.         } else if(iv & CHATxIP){
  72.             /* Channel A Transmit Int Pending */
  73.             hp = &Hdlc[2*dev];
  74.             htxint(hp);
  75.         } else if(iv & CHAEXT){
  76.             /* Channel A External Status Int */
  77.             hp = &Hdlc[2*dev];
  78.             hexint(hp);
  79.         } else if(iv & CHBRxIP){
  80.             /* Channel B Rcv Interrupt Pending */
  81.             hp = &Hdlc[(2*dev)+1];
  82.             hrxint(hp);
  83.         } else if(iv & CHBTxIP){
  84.             /* Channel B Transmit Int Pending */
  85.             hp = &Hdlc[(2*dev)+1];
  86.             htxint(hp);
  87.         } else if(iv & CHBEXT){
  88.             /* Channel B External Status Int */
  89.             hp = &Hdlc[(2*dev)+1];
  90.             hexint(hp);
  91.         }
  92.         /* Reset interrupt pending state */
  93.         write_scc(hp->ctl,R0,RES_H_IUS);
  94.         outportb(hsbase+CHANA+CTL,0);    /* Restore pointer to 0 */
  95.         outportb(hsbase+CHANB+CTL,0);    /* Restore pointer to 0 */
  96.     }
  97.     outportb(hsbase+CHANA+CTL,0);    /* Restore pointer to 0 */
  98.     outportb(hsbase+CHANB+CTL,0);    /* Restore pointer to 0 */
  99. }
  100. /* HDLC SIO External/Status interrupts
  101.  * The only one that can happen in this driver is a DCD change
  102.  */
  103. static void
  104. hexint(hp)
  105. register struct hdlc *hp;
  106. {
  107.     struct mbuf *rcvbuf;
  108.     struct phdr phdr;
  109.     char *cp;
  110.     int cnt,data;
  111.     register int ctl;
  112.  
  113.     ctl = hp->ctl;
  114.     data = hp->data;
  115.     hp->exints++;
  116.  
  117.     /* Allocate a receive buffer */
  118.     if((rcvbuf = alloc_mbuf(hp->bufsiz+sizeof(phdr))) == NULLBUF){
  119.         /* Alloc failed; refuse to proceed */
  120.         hp->nomem++;
  121.         write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  122.         write_scc(ctl,R0,RES_EXT_INT);
  123.         return;
  124.     }
  125.     /* Allow space for phdr descriptor on front */
  126.     cp = rcvbuf->data + sizeof(phdr);
  127.     cnt = 0;
  128.  
  129.     /* Disable DCDIE bit so we can track changes in DCD */
  130.     write_scc(ctl,R15,0);
  131.  
  132.     write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  133.     flushrx(data);
  134.     while((cnt = rx8530(ctl,data,cp,hp->bufsiz)) != -1){
  135.         if(cnt > 4){
  136.             /* Good frame */
  137.             hp->good++;
  138.             /* Toss crc */
  139.             rcvbuf->cnt = sizeof(phdr) + cnt - 1;
  140.             phdr.iface = hp->iface;
  141.             phdr.type = CL_AX25;
  142.             memcpy(rcvbuf->data,(char *)&phdr,sizeof(phdr));
  143.             enqueue(&Hopper,rcvbuf);
  144.             /* Replenish buffer */
  145.             rcvbuf = alloc_mbuf(hp->bufsiz + sizeof(phdr));
  146.         }
  147.         /* Start new buffer */
  148.         if(rcvbuf == NULLBUF)
  149.             break;    /* alloc failed */
  150.         cp = rcvbuf->data + sizeof(phdr);
  151.     }    
  152.     write_scc(ctl,R0,RES_EXT_INT);
  153.     write_scc(ctl,R15,DCDIE);    /* Re-enable DCD */
  154.     write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  155.  
  156.     /* Get rid of fragmentary buffer */
  157.     free_p(rcvbuf);
  158. }
  159. static void
  160. flushrx(data)
  161. register int16 data;
  162. {
  163.     register int i = 5;
  164.     while(i-- != 0)
  165.         (void)inportb(data);
  166. }
  167. /* HDLC receiver interrupt handler.
  168.  * Not used in this driver
  169.  */
  170. static void
  171. hrxint(hp)
  172. register struct hdlc *hp;
  173. {
  174. }
  175. /* HDLC transmit interrupt service routine
  176.  * Not used in this driver
  177.  */
  178. static void
  179. htxint(hp)
  180. register struct hdlc *hp;
  181. {
  182. }
  183.  
  184. /* (re)Initialize HDLC controller parameters */
  185. static void
  186. hdlcparam(hp)
  187. register struct hdlc *hp;
  188. {
  189.     char i_state;
  190.     register int16 ctl;
  191.  
  192.     /* Initialize 8530 channel for SDLC operation */
  193.     ctl = hp->ctl;
  194.     i_state = dirps();
  195.  
  196. #ifdef    foo
  197.     switch(ctl & 2){
  198.     case CHANA:
  199.         write_scc(ctl,R9,CHRA);    /* Reset channel A */
  200.         break;
  201.     case CHANB:
  202.         write_scc(ctl,R9,CHRB);    /* Reset channel B */
  203.         break;
  204.     }
  205.     mspause(1L);    /* Allow plenty of time for resetting */
  206. #endif
  207.  
  208.     /* Deselect interrupts for now */
  209.     write_scc(ctl,R1,0);
  210.     write_scc(ctl,R15,0);
  211.  
  212.     /* X1 clock, SDLC mode, Sync modes enable, parity disable */
  213.     write_scc(ctl,R4,X1CLK | SDLC | SYNC_ENAB);
  214.  
  215.     /* CRC preset 1, NRZ encoding, no active on poll, flag idle,
  216.      * flag on underrun, no loop mode, 8 bit sync
  217.      */
  218.     write_scc(ctl,R10,CRCPS|NRZ);
  219.  
  220.     /* 8530 gets both tx and rx clock from modem.
  221.      * By default, TRxC = transmit clock, RTxC = receive clock
  222.      * (swapped 11 Feb 1990 to use new DRSI wiring) UNLESS
  223.      * the 'r' parameter is specified
  224.      */
  225.     if(!hp->clkrev)
  226.         write_scc(ctl,R11,RCRTxCP | TCTRxCP);
  227.     else
  228.         write_scc(ctl,R11,RCTRxCP | TCRTxCP);
  229.  
  230.     /* Note: baud rate generator not used */
  231.  
  232.     /* Null out SDLC start address */
  233.     write_scc(ctl,R6,0);
  234.  
  235.     /* SDLC flag */
  236.     write_scc(ctl,R7,FLAG);
  237.  
  238.     /* DTR On, 8 bit TX chars, no break, TX enable, SDLC CRC,
  239.      * RTS off, TxCRC enable
  240.      */
  241.     write_scc(ctl,R5,DTR|Tx8|TxENAB|TxCRC_ENAB);
  242.  
  243.     /* 8 bit RX chars, auto enables off, no hunt mode, RxCRC enable,
  244.      * no address search, no inhibit sync chars, disable RX. Rx is
  245.      * started only by an actual DCD interrupt
  246.      */
  247.     write_scc(ctl,R3,RxENABLE|RxCRC_ENAB|Rx8);
  248.  
  249.     /* Dummy interrupt vector
  250.      * (This probably isn't necessary)
  251.      */
  252.     write_scc(ctl,R2,0);
  253.  
  254.     /* Enable only the external interrupts (modem interrupts) since
  255.      * polling is used for all actual tx/rx operations
  256.      */
  257.     write_scc(ctl,R1,EXT_INT_ENAB);
  258.  
  259.     /* Enable only DCD interrupts */
  260.     write_scc(ctl,R15,DCDIE);
  261.  
  262.     /* No reset, status low, master int enable, enable lower chain,
  263.      * no vector
  264.      */
  265.     write_scc(ctl,R9,MIE|NV);
  266.  
  267.     restore(i_state);
  268. }
  269. /* Attach a high speed iterface to the system
  270.  * argv[0]: hardware type, must be "hs"
  271.  * argv[1]: I/O address, e.g., "0x380"
  272.  * argv[2]: vector, e.g., "2"
  273.  * argv[3]: mode, must be "ax25"
  274.  * argv[4]: interface base label, e.g., "drsi0". Driver appends "a" and "b".
  275.  * argv[5]: receiver packet buffer size in bytes
  276.  * argv[6]: maximum transmission unit, bytes
  277.  * argv[7]: keyup delay, milliseconds
  278.  * argv[8]: persistence value, 0-255
  279.  * argv[9]: "r" to reverse sense of clock leads (optional)
  280.  */
  281. int
  282. hs_attach(argc,argv,p)
  283. int argc;
  284. char *argv[];
  285. void *p;
  286. {
  287.     register struct iface *if_hsa,*if_hsb;
  288.     struct hdlc *hp;
  289.     int dev;
  290.  
  291.     if(Nhs >= NHS){
  292.         tprintf("Too many hs controllers\n");
  293.         return -1;
  294.     }
  295.     if(if_lookup(argv[4]) != NULLIF){
  296.         tprintf(Existingiface,argv[4]);
  297.         return -1;
  298.     }
  299.     dev = Nhs++;
  300.  
  301.     /* Initialize hardware-level control structure */
  302.     Hs[dev].addr = htoi(argv[1]);
  303.     Hs[dev].vec = htoi(argv[2]);
  304.  
  305.     /* Save original interrupt vector */
  306.     Hs[dev].save.vec = getirq(Hs[dev].vec);
  307.     /* Set new interrupt vector */
  308.     if(setirq(Hs[dev].vec,Hshandle[dev]) == -1){
  309.         tprintf("IRQ %u out of range\n",Hs[dev].vec);
  310.         Nhs--;
  311.         return -1;
  312.     }
  313.     /* Create interface structures and fill in details */
  314.     if_hsa = (struct iface *)callocw(1,sizeof(struct iface));
  315.     if_hsb = (struct iface *)callocw(1,sizeof(struct iface));
  316.  
  317.     if_hsa->addr = if_hsb->addr = Ip_addr;
  318.     if_hsa->name = mallocw(strlen(argv[4])+2);
  319.     strcpy(if_hsa->name,argv[4]);
  320.     strcat(if_hsa->name,"a");
  321.     if_hsb->name = mallocw(strlen(argv[4])+2);
  322.     strcpy(if_hsb->name,argv[4]);
  323.     strcat(if_hsb->name,"b");
  324.     if_hsb->mtu = if_hsa->mtu = atoi(argv[6]);
  325.     if_hsb->type = if_hsa->type = CL_AX25;
  326.     if_hsa->dev = 2*dev;
  327.     if_hsb->dev = 2*dev + 1;
  328.     if_hsb->stop = if_hsa->stop = hs_stop;
  329.     if_hsb->output = if_hsa->output = ax_output;
  330.     if_hsb->raw = if_hsa->raw = hs_raw;
  331.     if_hsa->ioctl = if_hsb->ioctl = hs_ctl;
  332.  
  333.     if(strcmp(argv[3],"ax25") == 0){
  334.         if(Mycall[0] == '\0'){
  335.             tprintf("set mycall first\n");
  336.             free((char *)if_hsa);
  337.             free((char *)if_hsb);
  338.             return -1;
  339.         }        
  340.         if_hsb->send = if_hsa->send = ax_send;
  341.         if(if_hsb->hwaddr == NULLCHAR)
  342.             if_hsb->hwaddr = mallocw(AXALEN);
  343.         memcpy(if_hsb->hwaddr,Mycall,AXALEN);
  344.         if(if_hsb->ipcall == NULLCHAR)
  345.             if_hsb->ipcall = mallocw(AXALEN);
  346.         memcpy(if_hsb->ipcall,Mycall,AXALEN);
  347.         if(if_hsa->hwaddr == NULLCHAR)
  348.             if_hsa->hwaddr = mallocw(AXALEN);
  349.         memcpy(if_hsa->hwaddr,Mycall,AXALEN);
  350.         if(if_hsa->ipcall == NULLCHAR)
  351.             if_hsa->ipcall = mallocw(AXALEN);
  352.         memcpy(if_hsa->ipcall,Mycall,AXALEN);
  353.     } else {
  354.         tprintf("Mode %s unknown for interface %s\n",
  355.             argv[3],argv[4]);
  356.         free((char *)if_hsa);
  357.         free((char *)if_hsb);
  358.         return -1;
  359.     }
  360.     if_hsa->next = if_hsb;
  361.     if_hsb->next = Ifaces;
  362.     Ifaces = if_hsa;
  363.  
  364.     write_scc(Hs[dev].addr+CHANA+CTL,R9,FHWRES);
  365.     hp = &Hdlc[2*dev+1];
  366.     hp->ctl = Hs[dev].addr + CHANB + CTL;
  367.     hp->data = Hs[dev].addr + CHANB + DATA;
  368.     hp->bufsiz = atoi(argv[5]);
  369.     if(argc > 7)
  370.         hp->txdelay = atol(argv[7]);
  371.     else
  372.         hp->txdelay = 15L;
  373.     if(argc > 8)
  374.         hp->p = atoi(argv[8]);
  375.     else
  376.         hp->p = 64;
  377.     if(argc > 9 && argv[9][0] == 'r')
  378.         hp->clkrev = 1;
  379.     else
  380.         hp->clkrev = 0;
  381.     hp->iface = if_hsb;
  382.     hdlcparam(hp);
  383.     if_hsa->txproc = newproc("hs_tx",1024,hs_tx,0,hp,NULL,0);
  384.  
  385.     hp = &Hdlc[2*dev];
  386.     hp->ctl = Hs[dev].addr + CHANA + CTL;
  387.     hp->data = Hs[dev].addr + CHANA + DATA;
  388.     hp->bufsiz = atoi(argv[5]);
  389.     hp->txdelay = Hdlc[2*dev+1].txdelay;
  390.     hp->p = Hdlc[2*dev+1].p;
  391.     if(argc > 9 && argv[9][0] == 'r')
  392.         hp->clkrev = 1;
  393.     else
  394.         hp->clkrev = 0;
  395.     hp->iface = if_hsa;
  396.     hdlcparam(hp);
  397.     if_hsb->txproc = newproc("hs_tx",1024,hs_tx,0,hp,NULL,0);
  398.  
  399.     outportb(Hs[dev].addr + 4,0x08);    /*EAGLE INT GATE */
  400.     /* Clear mask (enable interrupt) in 8259 interrupt controller */
  401.     maskon(Hs[dev].vec);
  402.  
  403.     /* Initialize timing delay loop */
  404.     init_delay();
  405.     return 0;
  406. }
  407. static int
  408. hs_stop(iface)
  409. struct iface *iface;
  410. {
  411.     int dev;
  412.  
  413.     dev = iface->dev;
  414.     if(dev & 1)
  415.         return -1;    /* Valid only for the first device */
  416.     dev >>= 1;    /* Convert back into hs number */
  417.  
  418.     /* Turn off interrupts */
  419.     maskoff(Hs[dev].vec);
  420.  
  421.     /* Restore original interrupt vector */
  422.     setirq(Hs[dev].vec,Hs[dev].save.vec);
  423.  
  424.     /* Force hardware reset */
  425.     write_scc(Hs[dev].addr + CHANA+CTL,R9,FHWRES);
  426.     return 0;
  427. }
  428. /* Send raw packet */
  429. static int
  430. hs_raw(iface,bp)
  431. struct iface *iface;
  432. struct mbuf *bp;
  433. {
  434.  
  435.     struct hdlc *hp;
  436.  
  437.     dump(iface,IF_TRACE_OUT,CL_AX25,bp);
  438.     iface->rawsndcnt++;
  439.     iface->lastsent = secclock();
  440.     hp = &Hdlc[iface->dev];
  441.     hp->txpkts++;
  442.     enqueue(&hp->txq,bp);    /* Put on queue for hs_tx process */
  443.     return 0;
  444. }
  445.  
  446. /* High speed transmit process */
  447. void
  448. hs_tx(unused,hp1,a)
  449. int unused;
  450. void *hp1;
  451. void *a;    /* Unused */
  452. {
  453.     struct mbuf *bp,*nbp;
  454.     register int16 cnt;
  455.     register char *cp;
  456.     int16 ctl,data;
  457.     int txon = 0;    /* Transmitter on/off state */
  458.     struct hdlc *hp;
  459.     int i;
  460.  
  461.     hp = (struct hdlc *)hp1;
  462.     ctl = hp->ctl;
  463.     data = hp->data;
  464.  
  465.     for(;;){
  466.         /* Wait for work */
  467.         while(hp->txq == NULLBUF){
  468.             if(txon){
  469.                 /* No more frames, shut down tx */
  470.                 hstxoff(hp);
  471.                 txon = 0;
  472.                 /* Hold off to give other guy a chance to
  473.                  * respond
  474.                  */
  475.                 hp->deftime = msclock() + hp->txdelay + 500;
  476.             }
  477.             pwait(&hp->txq);
  478.         }
  479.         bp = dequeue(&hp->txq);
  480.         cnt = len_p(bp);
  481.         /* If buffer isn't contiguous (which is almost always
  482.          * the case) copy it to a new buffer for speed
  483.          */
  484.         if(bp->next != NULLBUF){
  485.             if((nbp = copy_p(bp,cnt)) == NULLBUF){
  486.                 hp->nomem++;
  487.                 free_p(bp);
  488.                 continue;
  489.             }
  490.             free_p(bp);
  491.             bp = nbp;
  492.         }
  493.         cp = bp->data;
  494.         /* Turn transmitter on if necessary */
  495.         if(!txon){
  496.             hstxon(hp);
  497.             txon = 1;
  498.         } else {
  499.             /* Else wait another txd for receiver to get ready again */
  500.             for(i=hp->txdelay;i != 0;i--)
  501.                 msdelay();
  502.         }
  503.         /* Initialize transmitter CRC */
  504.         write_scc(ctl,R0,RES_Tx_CRC);
  505.         for(;;){
  506.             /* Wait for the transmitter to become ready */
  507.             while(!(inportb(ctl) & Tx_BUF_EMP))
  508.                 ;
  509.             if(cnt-- == 0)
  510.                 break;
  511.             outportb(data,*cp++); /* Send the character */
  512.         }
  513.         write_scc(ctl,R0,RES_EOM_L);    /* Allow CRC generation */
  514.  
  515.         /* End of frame. Wait for TxEOM to go high, indicating start of
  516.          * CRC transmission. Note that we don't reset the transmit
  517.          * interrupt pending flag as one ordinarily would, since we're
  518.          * not using tx interrupts.
  519.          */
  520.         while(!(inportb(ctl) & TxEOM))
  521.             ;
  522.  
  523.         free_p(bp);
  524.     }
  525. }
  526.  
  527. /* Turn on high speed transmitter. Does p-persistence, then sends a dummy
  528.  * frame to allow for keyup delay. Returns with transmitter on and interrupts
  529.  * disabled
  530.  */
  531. static void
  532. hstxon(hp)
  533. struct hdlc *hp;
  534. {
  535.     int16 ctl;
  536.     int i;
  537.     long ca;
  538.     int32 t;
  539.  
  540.     ctl = hp->ctl;
  541.  
  542.     /* Defer logic. Wait until deftime is in the past (so we
  543.      * defer to any overheard CTS messages) AND the p-persistence
  544.      * dice roll succeeds. The computation of ca allows for clock
  545.      * rollover (which happens every 49+ days).
  546.      */
  547.     for(;;){
  548.         t = msclock();
  549.         ca = hp->deftime - t;
  550.         if(ca > 0){
  551.             mspause(ca);
  552.             continue;
  553.         }
  554.         hp->deftime = t;    /* Keep from getting too old */
  555.         if((rand() & 0xff) > uchar(hp->p)){
  556.             mspause((long)MSPTICK);
  557.             continue;
  558.         }
  559.         break;
  560.     }
  561.     /* Prevent distractions. In particular, block off the DCD interrupt
  562.      * so we don't hear our own carrier and hang in the interrupt handler!
  563.      * Note that simply disabling CPU interrupts isn't enough since
  564.      * the call to mspause will block and the kernel will re-enable
  565.      * them.
  566.      */
  567.     write_scc(ctl,R9,0);    /* Disable all SCC interrupts */
  568.     (void)dirps();        /* Return value is always 1 */
  569.  
  570.     /* Turn on carrier, enable transmitter */
  571.     write_scc(ctl,R5,TxCRC_ENAB | RTS | TxENAB | Tx8 | DTR);
  572.  
  573.     /* Delay for keyup interval */
  574.     for(i=hp->txdelay;i != 0;i--)
  575.         msdelay();
  576.  
  577. }
  578. /* Turn transmitter off at the end of a series of frames */
  579. static void
  580. hstxoff(hp)
  581. struct hdlc *hp;
  582. {
  583.     int cnt;
  584.     int16 ctl,data;
  585.  
  586.     ctl = hp->ctl;
  587.     data = hp->data;
  588.     /* To allow the SCC buffering to drain, we begin a dummy frame,
  589.      * then abort it
  590.      */
  591.     for(cnt=5;cnt != 0;cnt--){
  592.         while(!(inportb(ctl) & Tx_BUF_EMP))
  593.             ;
  594.         outportb(data,0);
  595.     }
  596.     write_scc(ctl,R0,SEND_ABORT);
  597.  
  598.     /* Turn off carrier and disable transmitter */
  599.     write_scc(ctl,R5,TxCRC_ENAB | Tx8 | DTR);
  600.     /* Re-Enable SCC interrupts */
  601.     write_scc(ctl,R9,MIE|NV);        
  602.     restore(1);    /* Turn interrupts back on */
  603. }
  604.  
  605. int
  606. dohs(argc,argv,p)
  607. int argc;
  608. char *argv[];
  609. void *p;
  610. {
  611.     register int i;
  612.     register struct hdlc *hp;
  613.  
  614.     for(i=0;i<2*Nhs;i++){
  615.         hp = &Hdlc[i];
  616.         if(tprintf("port %d: txpkts %lu ints %lu rxpkts %lu rxbytes %lu nomem %lu toobig %lu crcerr %lu aborts %lu overrun %lu\n",
  617.          i,hp->txpkts,hp->exints,hp->good,hp->rxbytes,
  618.          hp->nomem,hp->toobig,hp->crcerr,hp->aborts,
  619.          hp->overrun) == EOF)
  620.             break;
  621.     }
  622.     return 0;
  623. }
  624. static int32
  625. hs_ctl(iface,cmd,set,val)
  626. struct iface *iface;
  627. int cmd;
  628. int set;
  629. int32 val;
  630. {
  631.     register struct hdlc *hp;
  632.     int32 t,ca;
  633.  
  634.     hp = &Hdlc[iface->dev];
  635.     switch(cmd){
  636.     case PARAM_TXDELAY:    /* Tx keyup delay */
  637.         if(set)
  638.             hp->txdelay = val;
  639.         return hp->txdelay;
  640.     case PARAM_PERSIST:
  641.         if(set)
  642.             hp->p = val;
  643.         return uchar(hp->p);
  644.     case PARAM_MUTE:
  645.         /* Mute transmitter for specified # of ms */
  646.         if(set){
  647.             if(val == -1){
  648.                 /* Special case for duration of a CTS */
  649.                 val = hp->txdelay + 500;
  650.             }
  651.             hp->deftime = msclock() + val;
  652.         }
  653.         t = msclock();
  654.         ca = hp->deftime - t;
  655.         if(ca < 0){
  656.             hp->deftime = t;
  657.             ca = 0;
  658.         }
  659.         return ca;
  660.     }
  661.     return -1;
  662. }
  663. #ifdef    notdef        /* replaced with assembler in 8530.asm */
  664. /* Read data from the 8530 receiver.
  665.  * Returns when either a good frame is received, or when carrier drops.
  666.  * If a good frame is received, the length is returned; otherwise -1.
  667.  */
  668. int
  669. rx8530(ctl,data,buf,bufsize)
  670. int16 ctl,data;
  671. char *buf;
  672. int16 bufsize;
  673. {
  674.     int cnt = 0;
  675.     register char status;
  676.     char error;
  677.     register char *cp = buf;
  678.  
  679.     for(;;){
  680.         status = inportb(ctl);
  681.         if(!(status & DCD)){
  682.             cnt = -1;
  683.             break;
  684.         } else if(status & BRK_ABRT){
  685.             cp = buf;
  686.             cnt = 0;
  687.         } else if(status & Rx_CH_AV){
  688.             /* Receive character is ready, get it */
  689.             *cp++ = inportb(data);
  690.             if(++cnt > bufsize){
  691.                 /* Buffer overflow, start again */
  692.                 write_scc(ctl,R3,ENT_HM|RxENABLE|RxCRC_ENAB|Rx8);
  693.                 cp = buf;
  694.                 cnt = 0;
  695.             }
  696.         } else if((error = read_scc(ctl,R1)) & END_FR){
  697.             if(!(error & CRC_ERR))
  698.                 break;    /* Good frame! */
  699.             /* Bad frame, start again */
  700.             cp = buf;
  701.             cnt = 0;
  702.         }
  703.     }
  704.     return cnt;
  705. }
  706. #endif
  707.  
  708. static int32 Del_const;
  709.  
  710. /* Find the value of Del_const that will cause one execution of mloop()
  711.  * to take one millisecond
  712.  */
  713. static void
  714. init_delay()
  715. {
  716.     int32 start,delay;
  717.     register int i,j;
  718.     int success = 0;
  719.  
  720.     /* Start with small value to make things tolerable on slow machines */
  721.     Del_const = 10;
  722.     printf("Del_const = %lu\n",Del_const);
  723.     /* Limit the number of iterations in case we don't converge */
  724.     for(i=0;i<5;i++){
  725.         start = msclock();
  726.         for(j=0;j<1000;j++)
  727.             msdelay();
  728.         delay = msclock()-start;
  729.         printf("delay %lu\n",delay);
  730.         if(delay == 0){
  731.             /* Too fast for accurate measurement on coarse clk */    
  732.             Del_const *= 10;
  733.             printf("Del_const = %lu\n",Del_const);
  734.             continue;
  735.         }
  736.         Del_const = (Del_const * 1000)/delay;
  737.         printf("Del_const = %lu\n",Del_const);
  738.         if(delay > 950 && delay < 1050){
  739.             success = 1;
  740.             break;    /* Within 1 tick - Close enough */
  741.         }
  742.     }
  743.     if(!success)
  744.         tprintf("HS: Warning: auto delay set failed\n");
  745. }
  746. /* Delay for one millisecond (once calibrated by init_delay()) */
  747. static void
  748. msdelay()
  749. {
  750.     int32 i;
  751.  
  752.     for(i=Del_const;i !=0;i--)
  753.         ;
  754. }
  755.